﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Game.Card.Pay.ZFB.Helper;
using Game.Utils;
using Game.Kernel;
using Game.Facade;
using Game.Entity.Treasure;
using Game.Entity.Accounts;

namespace Game.Card.Pay.ZFB
{
    public partial class Alipay : AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //充值配置验证
            int payId = GameRequest.GetInt("id", 0);
            if(payId <= 0)
            {
                Response.Redirect("/Menu/Buy.aspx");
            }
            RoomCardConfig config = FacadeManage.aideTreasureFacade.GetRoomCardConfig(payId);
            if(config == null)
            {
                Response.Redirect("/Menu/Buy.aspx");
            }
            if(config.Amount <= 0 || config.RoomCard <= 0)
            {
                ShowInfo("充值配置参数错误");
                return;
            }

            //充值下单
            string orderID = PayHelper.GetOrderIDByPrefix("WapZFB");
            OnLineOrder onlineOrder = new OnLineOrder();
            onlineOrder.ShareID = 8001;
            onlineOrder.OrderID = orderID;

            UserTicketInfo info = userTicket;

            onlineOrder.OperUserID = info.UserID;
            onlineOrder.GameID = info.GameID;
            onlineOrder.OrderAmount = config.Amount;
            onlineOrder.IPAddress = GameRequest.GetUserIP();

            //生成订单
            Message umsg = FacadeManage.aideTreasureFacade.RequestOrderFK(onlineOrder, payId);
            if(!umsg.Success)
            {
                ShowInfo(umsg.Content);
                return;
            }

            #region 支付宝提交

            //接口名称
            string service = AlipayConfig.service;
            //合作者身份ID
            string partner = AlipayConfig.partner;
            //参数编码字符集
            string _input_charset = AlipayConfig.input_charset;
            //签名方式
            string sign_type = AlipayConfig.sign_type;
            //签名
            string sign = "";
            //服务器异步通知页面路径
            string notify_url = AlipayConfig.notify_url;
            //页面跳转同步通知页面路径
            string return_url = AlipayConfig.return_url;
            //商户网站唯一订单号
            string out_trade_no = orderID;
            //商品名称
            string subject = "充值房卡";
            //交易金额
            string total_fee = config.Amount.ToString();
            //卖家支付宝用户号
            string seller_id = AlipayConfig.seller_id;
            //支付类型
            string payment_type = AlipayConfig.payment_type;
            //商品展示网址
            string show_url = AlipayConfig.show_url;

            //把请求参数打包成数组
            SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
            sParaTemp.Add("partner", partner);
            sParaTemp.Add("seller_id", seller_id);
            sParaTemp.Add("_input_charset", _input_charset);
            sParaTemp.Add("service", service);
            sParaTemp.Add("payment_type", payment_type);
            sParaTemp.Add("notify_url", notify_url);
            sParaTemp.Add("return_url", return_url);
            sParaTemp.Add("out_trade_no", out_trade_no);
            sParaTemp.Add("subject", subject);
            sParaTemp.Add("total_fee", total_fee);
            sParaTemp.Add("show_url", show_url);

            //建立请求
            string sHtmlText = AlipaySubmit.BuildRequest(sParaTemp, "get", "确认");
            Response.Write(sHtmlText);

            #endregion
        }
    }
}